// index.js
import api from '../../utils/api'
import {
  isWithin24Hours
} from '../../utils/util'
Page({
  data: {
    colorList: [],
    carList: [],
    treeData: [],
    dataForm: {
      // operator: '13645557777',
      userCode: '',
      inviteCode: '',
      // dept: '',
      // idCard: '',
      // personName: '',
      // telNum: '',
      projectCorpName: '',
      // operatorName: '',
      // operatorTel: '',
      confirmer: '',
      confirmDate: '',
      projectDeptCode: '',
      projectDeptName: '',
      field2: '',
      field2Name: '',
      beginTime: '',
      endTime: '',
      persons: [{
        personName: '',
        idCard: '',
        // sexC: '',
        telNum: '',
      }],
      checkNotion: '',
      remark: '',
      file: '',

    },
    isSubmitting: false
  },
  onLoad() {
    api.orgTreeTree({
      orgLevel: '3'
    }).then(res => {

      // this.data.dataList = res.data.records
      let arr = []
      arr = this.flattenTree(res.data)
      arr.forEach(item => {
        item.value = item.id
      });
      console.log(arr, 666)
      this.setData({
        treeData: arr,
      });
    })
    api.getDictData({
      dictCode: 'SYGL/FKLX'
    }).then(res => {
      let arr = []
      res.data.forEach(item => {
        arr.push({
          name: item.dictName,
          value: item.dictValue
        })
      });
      this.setData({
        carList: arr,
      });
    })
  },
  flattenTree(treeData) {
    const result = [];
    const flatten = (node) => {
      result.push(node);
      if (node.childNode) {
        for (const child of node.childNode) {
          flatten(child);
        }
      }
    }
    for (const node of treeData) {
      flatten(node);
    }
    return result;
  },

  bindDateChange1: function (e) {
    this.data.dataForm.beginTime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindDateChange2: function (e) {
    this.data.dataForm.endTime = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChange(e) {

    this.data.dataForm.projectDeptCode = this.data.treeData[e.detail.value].value
    this.data.dataForm.projectDeptName = this.data.treeData[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  bindPickerChange2(e) {
    this.data.dataForm.field2 = this.data.carList[e.detail.value].value
    this.data.dataForm.field2Name = this.data.carList[e.detail.value].name
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput13: function (e) {
    this.data.dataForm.userCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  noInput: function (e) {
    this.data.dataForm.inviteCode = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput2: function (e) {
    this.data.dataForm.projectCorpName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput3: function (e) {
    this.data.dataForm.operatorName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput4: function (e) {
    this.data.dataForm.operatorTel = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput5: function (e) {
    this.data.dataForm.confirmer = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput6: function (e) {
    this.data.dataForm.confirmDate = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput7: function (e) {
    this.data.dataForm.checkNotion = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput8: function (e) {
    this.data.dataForm.remark = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput9: function (e) {
    this.data.dataForm.personName = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput10: function (e) {
    this.data.dataForm.idCard = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput11: function (e) {
    this.data.dataForm.dept = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  onInput12: function (e) {
    this.data.dataForm.telNum = e.detail.value
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  getAdd() {
    const {
      inviteCode,
      // dept,
      // idCard,
      // personName,
      // telNum,
      projectCorpName,
      // operatorName,
      // operatorTel,
      confirmer,
      confirmDate,
      projectDeptCode,
      field2,
      beginTime,
      endTime,
      userCode
    } = this.data.dataForm
    if (!inviteCode.trim()) {
      wx.showToast({
        title: '邀请码不能为空',
        icon: 'none',
      });
      return;
    }
    // if (!personName.trim()) {
    //   wx.showToast({
    //     title: '姓名不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!idCard.trim()) {
    //   wx.showToast({
    //     title: '身份证号不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!dept.trim()) {
    //   wx.showToast({
    //     title: '单位不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!telNum.trim()) {
    //   wx.showToast({
    //     title: '联系电话不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    if (!projectCorpName.trim()) {
      wx.showToast({
        title: '来访单位不能为空',
        icon: 'none',
      });
      return;
    }
    // if (!operatorName.trim()) {
    //   wx.showToast({
    //     title: '来访负责人姓名不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // if (!operatorTel.trim()) {
    //   wx.showToast({
    //     title: '来访负责人联系方式不能为空',
    //     icon: 'none',
    //   });
    //   return;
    // }
    if (!confirmer.trim()) {
      wx.showToast({
        title: '到访部室姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (!confirmDate.trim()) {
      wx.showToast({
        title: '到访部室联系方式不能为空',
        icon: 'none',
      });
      return;
    }
    if (!projectDeptCode.trim()) {
      wx.showToast({
        title: ' 到访部室不能为空',
        icon: 'none',
      });
      return;
    }
    if (!field2.trim()) {
      wx.showToast({
        title: '访客类型不能为空',
        icon: 'none',
      });
      return;
    }
    if (!beginTime.trim()) {
      wx.showToast({
        title: '有效期始不能为空',
        icon: 'none',
      });
      return;
    }
    if (!endTime.trim()) {
      wx.showToast({
        title: '有效期止不能为空',
        icon: 'none',
      });
      return;
    }
    let time = isWithin24Hours(beginTime, endTime)
    if (!time) {
      wx.showToast({
        title: '日期区间不能超过24小时',
        icon: 'none',
      });
      return;
    }
    if (!userCode.trim()) {
      wx.showToast({
        title: '被访人姓名不能为空',
        icon: 'none',
      });
      return;
    }
    if (this.data.dataForm.persons.length == 0) {
      wx.showToast({
        title: '访客信息不能为空',
        icon: 'none',
      });
      return;
    }
    let num = 0
    this.data.dataForm.persons.forEach(item => {
      if (!item.idCard || !item.personName || !item.telNum) {
        num++
      }
    });
    if (num > 0) {
      wx.showToast({
        title: '访客信息内容不能为空',
        icon: 'none',
      });
      return;
    }
    // this.data.dataForm.persons = [{
    //   // dept: this.data.dataForm.dept,
    //   idCard: this.data.dataForm.idCard,
    //   personName: this.data.dataForm.personName,
    //   telNum: this.data.dataForm.telNum
    // }]
    if (this.data.isSubmitting) return; // 如果正在提交，则不再处理新的提交请求
    // 设置为正在提交状态
    this.setData({
      isSubmitting: true
    });
    wx.showLoading({
      title: '提交中...',
    });
    api.applyBatchAddWeChat(this.data.dataForm).then(res => {
      setTimeout(function () {
        wx.hideLoading();
        wx.redirectTo({
          url: '/pages/visitorList/visitor', //附带两个参数过去
        })
        // this.setData({
        //   isSubmitting: false
        // }); // 请求成功后重置提交状态

      }, 3000)

    }).catch(e => {
      this.setData({
        isSubmitting: false
      }); // 请求失败后重置提交状态
    })
  },
  showList() {
    wx.redirectTo({
      url: '/pages/visitorList/visitor', //附带两个参数过去
    })
  },
  bindPickerChangeSex: function (e) {
    const index = e.currentTarget.dataset.index;
    const name = e.currentTarget.dataset.name;

    if (name == 'personName') {
      this.data.dataForm.persons[index].personName = e.detail.value;
    }
    if (name == 'idCard') {
      this.data.dataForm.persons[index].idCard = e.detail.value;
    }
    if (name == 'telNum') {
      this.data.dataForm.persons[index].telNum = e.detail.value;
    }
    // if (name == 'sexC') {
    //   this.data.dataForm.persons[index].sexC = this.data.sexList[e.detail.value].value;
    //   this.data.dataForm.persons[index].sexCName = this.data.sexList[e.detail.value].name;
    // }
    this.setData({
      dataForm: this.data.dataForm
    })

  },
  personsAdd() {
    this.data.dataForm.persons.push({
      personName: '',
      idCard: '',
      // sexC: '',
      telNum: '',
    })
    this.setData({
      dataForm: this.data.dataForm
    })
  },
  personsDet: function (e) {
    const index = e.currentTarget.dataset.index;
    this.data.dataForm.persons.splice(index, 1);
    this.setData({
      dataForm: this.data.dataForm
    })

  },
})